package com.gonsin.gview.logic.enums;

import com.gonsin.base.exceptions.IResultCode;

import java.util.*;
import java.util.stream.Collectors;

public enum GViewResultCodes implements IResultCode {

    OK(200, "成功"),
    EXIST(300, "重复添加或者注册"),
    USER_CODE_ERROR(301, "验证码错误"),
    USER_TIMEOUT(302, "登录超时"),

    ;

    private int value;

    private String name;

    GViewResultCodes(int mode, String name) {
        this.value = mode;
        this.name = name;
    }

    @Override
    public int getValue() {
        return value;
    }

    @Override
    public String getName() {
        return name;
    }

    private static Map<Integer, GViewResultCodes> paramKeysMap;


    private static List<Map<String, Object>> allResultCodes = new ArrayList<>();
    static {
        paramKeysMap = new HashMap<>();
        GViewResultCodes[] keys = GViewResultCodes.values();
        for (GViewResultCodes key : keys) {
            paramKeysMap.put(key.getValue(), key);
        }


        allResultCodes.addAll(Arrays.stream(values()).map(a -> {
            Map<String, Object> m = new HashMap<>();
            m.put("value", a.getValue());
            m.put("name", a.getName());
            return m;
        }).collect(Collectors.toList()));
    }

    public static GViewResultCodes get(int code) {
        return paramKeysMap.get(code);
    }

    /**
     * 其他模块通过该方法将自身的枚举值传递给BaseResultCode
     * @param otherCodes
     */
    public static void add(IResultCode[] otherCodes){
        allResultCodes.addAll(Arrays.stream(otherCodes).map(a -> {
            Map<String, Object> m = new HashMap<>();
            m.put("value", a.getValue());
            m.put("name", a.getName());
            return m;
        }).collect(Collectors.toList()));
    }


    /**
     * 获取列表
     * @return
     */
    public static List<Map<String, Object>> list(){
        return allResultCodes;
    }
}
